package com.test.www.service;

import com.test.www.util.FileUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <p>学生信息录入页面</p>
 * @author Bosen
 * 2020/12/17 21:42
 */
public class AddStudent {
    public AddStudent(){
        JFrame jFrame = new JFrame("学生成绩管理系统（信息录入）");
        JPanel jPanel = new JPanel(new GridLayout(9,2));
        JButton jButton1 = new JButton("这里是学生录入信息页面");
        JButton jButton2 = new JButton("返回");
        JPopupMenu jPopupMenu = new JPopupMenu();

        JLabel jLabel1 = new JLabel("学  号：",JLabel.CENTER);
        JTextField text1 = new JTextField();
        JLabel jLabel2 = new JLabel("名  字：",JLabel.CENTER);
        JTextField text2 = new JTextField();
        JLabel jLabel3 = new JLabel("数  学：",JLabel.CENTER);
        JTextField text3 = new JTextField();
        JLabel jLabel4 = new JLabel("语  文：",JLabel.CENTER);
        JTextField text4 = new JTextField();
        JLabel jLabel5 = new JLabel("英  语：",JLabel.CENTER);
        JTextField text5 = new JTextField();
        JLabel jLabel6 = new JLabel("物  理：",JLabel.CENTER);
        JTextField text6 = new JTextField();
        JLabel jLabel7 = new JLabel("计算机：",JLabel.CENTER);
        JTextField text7 = new JTextField();
        JLabel jLabel8 = new JLabel("        ",JLabel.CENTER);
        JButton jButton3 = new JButton("确认录入");

        Font font = new Font("宋体",Font.BOLD,20);
        jLabel1.setFont(font);
        jLabel2.setFont(font);
        jLabel3.setFont(font);
        jLabel4.setFont(font);
        jLabel5.setFont(font);
        jLabel6.setFont(font);
        jLabel7.setFont(font);

        jFrame.setSize(850, 550);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        // 返回
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Home();
                jFrame.dispose();
            }
        });

        // 确认录入
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jPopupMenu.removeAll();
                try {
                    String id = text1.getText();
                    String username = text2.getText();

                    if(id.equals("") || username.equals("")){
                        int z = 1/0;
                    }

                    double math = Double.valueOf(text3.getText());
                    double chinese = Double.valueOf(text4.getText());
                    double english = Double.valueOf(text5.getText());
                    double physics = Double.valueOf(text6.getText());
                    double computer = Double.valueOf(text7.getText());

                    FileUtil.getInstance().addStudent(id,username,math,chinese,english,physics,computer);

                    jPopupMenu.add("学生信息录入成功");
                } catch (Exception ex) {
                    ex.printStackTrace();
                    jPopupMenu.add("学生信息录入失败");
                }
                jPopupMenu.setForeground(Color.RED);
                jPopupMenu.show(jFrame,360,250);
            }
        });

        jPanel.add(jButton1);
        jPanel.add(jButton2);
        jPanel.add(jLabel1);
        jPanel.add(text1);
        jPanel.add(jLabel2);
        jPanel.add(text2);
        jPanel.add(jLabel3);
        jPanel.add(text3);
        jPanel.add(jLabel4);
        jPanel.add(text4);
        jPanel.add(jLabel5);
        jPanel.add(text5);
        jPanel.add(jLabel6);
        jPanel.add(text6);
        jPanel.add(jLabel7);
        jPanel.add(text7);
        jPanel.add(jLabel8);
        jPanel.add(jButton3);

        jFrame.add(jPanel);
        jFrame.setVisible(true);
    }
}
